package com.stu.serive.impl;

import com.stu.constant.StatusConstant;
import com.stu.context.BaseContext;
import com.stu.mapper.ViolationMapper;
import com.stu.pojo.ViolationApplication;
import com.stu.pojo.ViolationInformation;
import com.stu.serive.ViolationService;
import com.stu.serive.aTService;
import com.stu.vo.ViolationApplicationVo;
import com.stu.vo.ViolationInformationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ViolationImpl implements ViolationService {

    @Autowired
    private ViolationMapper violationMapper;

    /**
     * 查询处分信息
     * @return
     */
    public List<ViolationInformationVo> selectViolationInformation() {
        //创建vo数组
        List<ViolationInformationVo> violationInformationVoList = new ArrayList<>();
        //获取当前学生id
        Long currentId = BaseContext.getCurrentId();
        //获取实体
        List<ViolationInformation> violationInformationList =
                violationMapper.selectViolationInformation(Math.toIntExact(currentId));
        //修改审核状态
        for (ViolationInformation violationInformation : violationInformationList) {
            //创建vo对象
            ViolationInformationVo vo = new ViolationInformationVo();
            //赋值
            BeanUtils.copyProperties(violationInformation,vo);
            //判断是否可以解除该处分
            if (vo.getViolationEnd().isBefore(LocalDate.now())){
                vo.setExamineStatus("解除开启");
            }else{
                vo.setExamineStatus("解除禁止");
            }
            //获取学生名字
            vo.setName(violationMapper.selectName(currentId));
            violationInformationVoList.add(vo);
        }

        return violationInformationVoList;
    }

    /**
     * 添加解除处分的申请
     * @param violationId
     */
    public void insertViolationApplication(Integer violationId) {
        //获取当前学生id
        Long currentId = BaseContext.getCurrentId();
        //创建实体
        ViolationApplication violationApplication = new ViolationApplication();
        //赋值
        violationApplication.setStuId(Math.toIntExact(currentId));
        violationApplication.setViolationId(violationId);
        violationApplication.setApplicationTime(LocalDateTime.now());
        violationMapper.insertViolationApplication(violationApplication);
    }

    /**
     * 查询处分解除进度
     * @return
     */
    public List<ViolationApplicationVo> selectViolationApplication() {
        //获取当前学生id
        Long currentId = BaseContext.getCurrentId();
        //创建实体数组
        List<ViolationApplicationVo> violationApplicationVoList = new ArrayList<>();
        //获取实体值
        List<ViolationApplication> violationApplicationList = violationMapper.selectViolationApplication(currentId);
        //遍历数组
        for (ViolationApplication violationApplication : violationApplicationList) {
            ViolationApplicationVo vo = new ViolationApplicationVo();
            BeanUtils.copyProperties(violationApplication, vo);
            //判断进度
            if( violationApplication.getAdminExamine() == 1){
                vo.setStatus(StatusConstant.STATUS_1);
            }else if( violationApplication.getAdminExamine() == 2){
                vo.setStatus(StatusConstant.STATUS_2);
            }else{
                vo.setStatus(StatusConstant.STATUS_0);
            }
            violationApplicationVoList.add(vo);
        }


        return violationApplicationVoList;
    }

    /**
     * 查询所有解除处分申请
     * @return
     */
    public List<ViolationApplicationVo> selectViolationApplicationAll() {
        return violationMapper.selectViolationApplicationAll();
    }
}
